package es.dk.hl.view.helper
{
	import com.roguedevelopment.objecthandles.ObjectHandles;
	
	import es.dk.hl.event.WallEvent;
	import es.dk.hl.view.DecoradorImage;
	import es.dk.hl.view.Wall;
	import es.dk.hl.vo.PhotoVO;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.DragSource;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;

	public class WallHelper implements IWallHelper
	{
		public var wallBackgroundChoosed:Boolean = false;
		
		public var view : Wall;
		
		public function WallHelper()
		{
		}
		
		public function getView():UIComponent 
		{
			return UIComponent(view);
		}
		 
		public function creationCompleteHandler() : void
		{
			var wallEvent:WallEvent = new WallEvent(WallEvent.WallEvent_ListenClickPhoto,this)
			wallEvent.dispatch()
		}		
		  	
		private var imageProxy:DecoradorImage;
        private var beforeDoDragX:Number = 0 
        private var beforeDoDragY:Number = 0;
            		
        public function dragDropHandler(event:DragEvent):void {
			event.dragInitiator.x = event.currentTarget.mouseX - beforeDoDragX;
			event.dragInitiator.y = event.currentTarget.mouseY - beforeDoDragY;				
        }     
            
        public function dragEnterHandler(event:DragEvent):void {            	
           if (event.dragSource.hasFormat("img")) {
                DragManager.acceptDragDrop(Canvas(event.currentTarget));    
            }                
        }	
      	    
        private var decoradorImageUsing:DecoradorImage;
        
        private function mouseClickHandler(event:MouseEvent):void  {
			Alert.show("click");
        }	
        
        private function mouseDoubleClickHandler(event:MouseEvent):void  {
			Alert.show("doble click");
        }  
        	        	       				
        private function mouseMoveHandler(event:MouseEvent):void  {  
        	            	              
            decoradorImageUsing=DecoradorImage(event.currentTarget);
            
            var ds:DragSource = new DragSource();
            ds.addData(decoradorImageUsing, "img");               

            imageProxy = new DecoradorImage();
            imageProxy.source = (event.currentTarget as DecoradorImage).source;
            imageProxy.height=(event.currentTarget as DecoradorImage).height
            imageProxy.width=(event.currentTarget as DecoradorImage).width
            
            beforeDoDragX = decoradorImageUsing.mouseX;
            beforeDoDragY = decoradorImageUsing.mouseY;
            
            DragManager.doDrag(decoradorImageUsing, ds, event,imageProxy);
        }
		                                       		
		public function addToWall(decorator:Object):void  {
			if (PhotoVO.getTypePhoto(DecoradorImage(decorator).photo) == PhotoVO.wall){			
				view.setStyle("backgroundImage",DecoradorImage(decorator).source);
			}
			if (PhotoVO.getTypePhoto(DecoradorImage(decorator).photo) == PhotoVO.item){
				var decoradorImage:DecoradorImage = new DecoradorImage();
				decoradorImage.source = DecoradorImage(decorator).source;
				decoradorImage.percentHeight = 100;
//				decoradorImage.scaleContent = false;
//	        	decoradorImage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);        	
//	        	decoradorImage.addEventListener(MouseEvent.CLICK,mouseClickHandler);
//	        	decoradorImage.addEventListener(MouseEvent.DOUBLE_CLICK,mouseDoubleClickHandler);
//	        	decoradorImage.useHandCursor = true;
	        	decoradorImage.buttonMode = true;
								
				var oHdecoradorImage:ObjectHandles = new ObjectHandles();
				oHdecoradorImage.allowRotate = true;
				oHdecoradorImage.width = DecoradorImage(decorator).width;
				oHdecoradorImage.height = DecoradorImage(decorator).height;
				oHdecoradorImage.addEventListener(MouseEvent.DOUBLE_CLICK,mouseDoubleClickHandler);
				oHdecoradorImage.buttonMode = true;
				
				oHdecoradorImage.addChild(decoradorImage);

				view.addChild(oHdecoradorImage);
			}			
		}	
	}
}